package com.xca.znb.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.contact.UpdatePasswordContact;
import com.xca.znb.model.impl.UpdatePasswordImpl;
import com.xca.znb.presenter.UpdatePasswordPresenter;

public class UpdatePayPassword extends AppCompatActivity implements UpdatePasswordContact {
    private UpdatePasswordPresenter presenter ;
    private ImageButton payPawBack ;
    private TextView showPhone ;
    private EditText  newPaw , newPawAgain ;
    private Button Submit ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_pay_password);
        payPawBack = findViewById(R.id.payPaw_back_btn);
        showPhone = findViewById(R.id.show_login_phone_number) ;
        newPaw = findViewById(R.id.new_pay_password);
        newPawAgain = findViewById(R.id.sure_pay_password);
        Submit = findViewById(R.id.submit_pay_password);

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        showPhone.setText(phone);
        UpdatePasswordImpl updatePassword = new UpdatePasswordImpl(this);
        presenter = new UpdatePasswordPresenter(this,updatePassword);
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPsw = newPaw.getText().toString();
                String newAgain = newPawAgain.getText().toString();

                if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newAgain)){
                    Toast.makeText(UpdatePayPassword.this, "所有字段都必须填写", Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (oldPsw.isEmpty()){
//                    Toast.makeText(UpdatePayPassword.this, "旧密码未填写", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (newPsw.isEmpty()){
                    Toast.makeText(UpdatePayPassword.this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (newAgain.isEmpty()){
                    Toast.makeText(UpdatePayPassword.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!newPsw.equals(newAgain)){
                    Toast.makeText(UpdatePayPassword.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ((!newPsw.matches("\\d{6}" )) && ((!newAgain.matches("\\d{6}")))){
                    Toast.makeText(UpdatePayPassword.this, "支付密码必须为6位", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!newPsw.matches("[0-9]{6}")  && !newAgain.matches("[0-9]{6}")){
                    Toast.makeText(UpdatePayPassword.this, "支付密码必须为数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                showPasswordDialog();
            }
        });


        payPawBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void defaultSetPassword() {
        Toast.makeText(this, "初始支付密码默认为：000000", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void oldPasswordWrong() {
        Toast.makeText(this, "未获取到原支付密码", Toast.LENGTH_SHORT).show();
    }
    private void showPasswordDialog() {
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        new AlertDialog.Builder(UpdatePayPassword.this)
                .setTitle("请输入原密码")
                .setView(input)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                        String phone = sharedPreferences.getString("phone" , "");
                        String enteredPassword = input.getText().toString();
                        boolean isOldPaw = presenter.updatePaySuccessful(phone , enteredPassword);
                        if (isOldPaw) {
                            String newPsw = newPaw.getText().toString();
                            presenter.updatePayPassword(phone ,newPsw);
                            boolean isUpdate = presenter.oldPawIsTure(phone,newPsw);
                            if (isUpdate){
                                finish();
                                Toast.makeText(UpdatePayPassword.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(UpdatePayPassword.this, "原密码错误", Toast.LENGTH_SHORT).show();
                            }
                            //finish();
                        } else {
                            // 密码错误，显示提示
                            Toast.makeText(UpdatePayPassword.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}
